﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Entities;

namespace QLTB_CTK34
{
	public partial class FrmQuanLiNhatKySuDung : Form
	{
		public FrmQuanLiNhatKySuDung()
		{
			InitializeComponent();
			dgvNhatKiSuDung.AutoGenerateColumns = false;
		}
		void LoadDataGridview()
		{
			dgvNhatKiSuDung.DataSource = QLNhatKy.GetAll();
		}

		private void formNhatKySuDung_Load(object sender, EventArgs e)
		{
			dgvNhatKiSuDung.DataSource = QLNhatKy.GetAll();
			cbbLoaiTimKiem.SelectedIndex = 0;
		}
		string maNK;
		private void dgvNhatKiSuDung_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			GetMaNK(e);
			row = e.RowIndex;
			if (e.RowIndex == -1 && e.ColumnIndex > -1)
			{
				QLThietBi.XapXep(dgvNhatKiSuDung, e);
			}
		}
		int row = 0;
		void GetMaNK(DataGridViewCellEventArgs e)
		{
			maNK = e.RowIndex != -1 ? dgvNhatKiSuDung.Rows[e.RowIndex].Cells["MaNhatKy"].Value.ToString() : "";
		}

		private void bntXoaNhatKi_Click(object sender, EventArgs e)
		{
			if (dgvNhatKiSuDung.SelectedRows.Count > 0)
				for (int i = 0; i < dgvNhatKiSuDung.SelectedRows.Count; i++)
					QLNhatKy.DeleteByMaNK(int.Parse(maNK));
			LoadDataGridview();
			QLNhatKy.ThemNhatKy("Xóa nhật kí sử dụng");
		}

		private void txtThongTinTimKiem_TextChanged(object sender, EventArgs e)
		{
			QLNhatKy.DKTimKiem dk = (QLNhatKy.DKTimKiem)cbbLoaiTimKiem.SelectedIndex;
			QLNhatKy.TimKiem(dgvNhatKiSuDung, cbbLoaiTimKiem, dk, txtThongTinTimKiem.Text);
		}

		private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(maNK))
			{
				if (dgvNhatKiSuDung.SelectedRows.Count > 0)
					for (int i = 0; i < dgvNhatKiSuDung.SelectedRows.Count; i++)
						QLNhatKy.DeleteByMaNK(int.Parse(maNK));
				LoadDataGridview();
			}
		}

        private void tsbtnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tsbtnRefresh_Click(object sender, EventArgs e)
        {
            LoadDataGridview();
        }

		private void dgvNhatKiSuDung_MouseDown(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Right)
			{
				var a = dgvNhatKiSuDung.HitTest(e.X, e.Y);
				dgvNhatKiSuDung.ClearSelection();
				dgvNhatKiSuDung.Rows[a.RowIndex].Selected = true;
			}
		}
		
	}
}
